#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
void sys_err(const char* str)
{
    perror(str);
    exit(1);
}
int main(int argc,char* argv[])
{
    int sockfd;
    char buff[BUFSIZ];
    int n;
    socklen_t addr_len;
    struct sockaddr_in server_addr;
    bzero(&server_addr,sizeof(server_addr));
    server_addr.sin_family=AF_INET;
    server_addr.sin_port=htons(8080);
    inet_pton(AF_INET,"127.0.0.1",&server_addr.sin_addr.s_addr);
    
    sockfd=socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd<0)
    {
        sys_err("socket()");
    }

    // int ret=connect(sockfd,(const struct sockaddr*)&server_addr,sizeof(server_addr));
    // if(ret!=0)
    // {
    //     sys_err("connect()");
    // }
    //int count=10;
    while(1)
    {
        fgets(buff,sizeof(buff),stdin);
        sendto(sockfd,buff,strlen(buff),0,(struct sockaddr*)&server_addr,sizeof(server_addr));
        n = recvfrom(sockfd,buff,sizeof(buff),0,NULL,0);
        write(STDOUT_FILENO,buff,n);
        // write(sockfd,"hello",6);
        // ret=read(sockfd,buff,sizeof(buff));
        // write(STDOUT_FILENO,buff,ret);
        ///sleep(1);
    }
    close(sockfd);
    return 0;
}